package com.gomore.opple.module.account;

import android.os.Bundle;
import android.view.KeyEvent;

import com.gomore.opple.R;
import com.gomore.opple.model.EventReflashShoppingCar;
import com.gomore.opple.model.Good;
import com.gomore.opple.module.BaseActivity;
import com.gomore.opple.module.IntentStart;

import javax.inject.Inject;

import de.greenrobot.event.EventBus;

/**
 * Created by asus on 2017/5/9.
 */
public class AccountActivity extends BaseActivity {
    @Inject
    AccountPresenter accountPresenter;

    private AccountFragment accountFragment;
    private Good good;

    @Override
    protected void initalizeViewData() {
        super.initalizeViewData();
        if (getIntent() != null && getIntent().getSerializableExtra(IntentStart.GOOD) != null) {
            good = (Good) getIntent().getSerializableExtra(IntentStart.GOOD);
        }
    }

    @Override
    protected void initalizeViews() {
        if (accountFragment == null) {
            accountFragment = AccountFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putSerializable(IntentStart.GOOD, good);
            accountFragment.setArguments(bundle);
            replaceFragment(accountFragment, false, "account_view");
        }
    }

    @Override
    protected void initializeDependencyInjector() {
        DaggerAccountComponent.builder()
                .dataRepositoryComponent(getRepositoryComponent())
                .accountPresenterModule((new AccountPresenterModule(accountFragment)))
                .build()
                .inject(this);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.activity_account;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            EventReflashShoppingCar eventReflashShoppingCar = new EventReflashShoppingCar();
            eventReflashShoppingCar.setReflashShoppingCar(true);
            EventBus.getDefault().post(eventReflashShoppingCar);
        }
        return super.onKeyDown(keyCode, event);
    }
}
